package NewCharacter;

import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class Demo3_CommonFunctionalInterface {

    //测试 supplier 函数式接口
    public void useSupplier(){

        Random random = new Random();

//        Supplier supplier = () -> {
//            return random.nextInt();
//        };

        Supplier supplier = () -> random.nextInt();

        System.out.println(supplier.get());
        System.out.println(supplier.get());
        System.out.println(supplier.get());
        System.out.println(supplier.get());
        System.out.println(supplier.get());
        System.out.println(supplier.get());


    }


    public void useConsumer(){
//        Consumer<String> consumer = (str) -> {
//            str.toLowerCase();
//        };

        Consumer<String> consumer = str -> {
            System.out.println(str.toLowerCase());
        };

        consumer.accept("HeLLO");
        consumer.accept("wOrlD");
        consumer.accept("Ztq");
    }

    public void useFunction(){

        Function<String,Integer> function = num -> Integer.parseInt(num);

        System.out.println((function.apply("123")+100));
        System.out.println(function.apply("456"));
        System.out.println(function.apply("321"));
        System.out.println(function.apply("111"));

    }

    public void usePredicate(int number){


        Predicate<Integer> predicate = num -> num % 2 == 0;

        if(predicate.test(number)){
            System.out.println(number+"偶数");
        }else{
            System.out.println(number+"奇数");
        }

    }

    public static void main(String[] args) {

        Demo3_CommonFunctionalInterface common = new Demo3_CommonFunctionalInterface();

//        common.useSupplier();
//        common.useConsumer();
//        common.useFunction();
        common.usePredicate(100);
        common.usePredicate(103);
        common.usePredicate(520);
    }

}
